package com.example.test.controller;

import com.example.test.bean.UserBean;
import com.example.test.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能 controller层
 */
@Controller
public class UserController {
    //自动装配 bean
    @Autowired
    UserService userservice;


     //添加用户
    @PostMapping("/addUser")
    @ResponseBody
    public Map addUsers(UserBean userBean){
        int flag = userservice.insertUser(userBean);
        Map<String,Object> map = new HashMap<String,Object>();
            if(flag == 1){
                map.put("msg","增加用户成功！");
                return map;
            }else{
                map.put("msg","增加用户失败！");
                return map;
            }
    }
    //id 删除用户
   /* @RequestMapping(value = "/deleteUser+{id}")
    public String deletUser(@PathVariable("id") String id,ModelMap modelMap){
        int i = userservice.deleteUser(id);
        List<UserBean> allUser = userservice.getAllUser();
        modelMap.addAttribute("userlist",allUser);
        if(i==1){
            return "users";
        }else{
            return "error";
        }
    }*/
    //id 查询用户
    @RequestMapping(value = "/user+{id}")
    public String getUserByid(@PathVariable("id") String id,ModelMap modelmap){
        UserBean user = userservice.getUserById(id);
        modelmap.addAttribute("user",user);
        return "userInfo";
    }
    //修改
   /* @RequestMapping(value = "/modifyUser")
    @ResponseBody
    public Map updateUaer(UserBean user){
        Map<String,Object> map = new HashMap<String,Object>();
        int i = userservice.updateUser(user);
        if(i==1){
            map.put("msg","修改信息成功");
            return map;
        }else {
            map.put("msg","修改信息失败");
            return map;
        }
    }*/
    //分页
    @RequestMapping("/users")
    public String index(Model model, @RequestParam(defaultValue = "2") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<UserBean> pageInfo = userservice.selectUser4Page(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userlist",pageInfo.getList());
        return "users";
    }
    //注销
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
    //前往登录页面
    //前往登录页面
//    @RequestMapping("/add")
//    public String add(){
//        return "add";
//    }

    //前往登入页面
    @RequestMapping("/add")
    public String index1(Model model, @RequestParam(defaultValue = "2") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
       PageInfo<UserBean> pageInfo = userservice.selectUser4Page(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("userlist",pageInfo.getList());
        return "add";

    }
}
